/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-10
 * Time: 23:33
 */



class ListNode {
    int val;
    ListNode next;
    ListNode() {
    }
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode sortedList = swapPairs(head.next.next);
        ListNode headNext = head.next;
        headNext.next = head;
        head.next = sortedList;
        return headNext;
    }
}

class Solution2 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(head==null) return head;
        ListNode dummyHead = new ListNode(-1,head);
        ListNode prev = dummyHead;
        ListNode cur = head;
        for(int i =0;i<left-1;++i){
            prev = prev.next;
            cur = cur.next;
        }
        for(int i =0;i<right-left;++i){
            ListNode curNext = cur.next;
            cur.next = curNext.next;
            curNext.next = prev.next;
            prev.next = curNext;
        }
        return dummyHead.next;
    }
}



public class Test {



}
